<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="一次完整的MYSQL学习和读书笔记内容">
<meta property="og:type" content="article">
<meta property="og:title" content="《MySQL必知必会》读书笔记">
<meta property="og:url" content="https://whitestore.top/2021/10/16/mysqlbizhi/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="一次完整的MYSQL学习和读书笔记内容">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-16T12:21:39.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.232Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="mysql入门的学习方法">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://whitestore.top/2021/10/16/mysqlbizhi/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>《MySQL必知必会》读书笔记 | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2021/10/16/mysqlbizhi/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          《MySQL必知必会》读书笔记
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-10-16 20:21:39" itemprop="dateCreated datePublished" datetime="2021-10-16T20:21:39+08:00">2021-10-16</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/10/16/mysqlbizhi/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/10/16/mysqlbizhi/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>
            <div class="post-description">一次完整的MYSQL学习和读书笔记内容</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="《MySQL必知必会》读书笔记"><a href="#《MySQL必知必会》读书笔记" class="headerlink" title="《MySQL必知必会》读书笔记"></a>《MySQL必知必会》读书笔记</h1><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>​    第一次完整的技术书籍的读书笔记，这本书200多页，看起来轻松又简单，当然因为内容本身非常基础的缘故，这本书我也只是翻了一遍，等接触到具体内容的时候可以拿起来再看看，看这本书的意义就在于此。</p>
<h1 id="资源链接："><a href="#资源链接：" class="headerlink" title="资源链接："></a>资源链接：</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">链接：https:&#x2F;&#x2F;pan.baidu.com&#x2F;s&#x2F;1RnsH_-HjTCgKOKlxWg4dTg </span><br><span class="line">提取码：6nta </span><br><span class="line">--来自百度网盘超级会员V6的分享</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<h1 id="推荐语"><a href="#推荐语" class="headerlink" title="推荐语"></a>推荐语</h1><p>​    这本书十分的基础，适合没有学过数据库的小白学习，当然不要过于纠结版本特性，只需要了解基本的sql操作即可。这本书可以让新手快速上手mysql，非常典型的一本入门指导书。</p>
<p>​    对于已经熟悉mysql的人，这本书可以作为回顾使用，包括mysql里面基本内容以及需要学习的重点，后续介绍的触发器，存储过程，游标等内容可能可能会觉得平时工作用的不多但是其实也是十分重要的内容。</p>
<p>​    当然这本书<strong>不建议购买实体书</strong>。买个电子版的翻一翻还是不错的，有种快速学会一本书所有内容的畅快感。</p>
<h1 id="个人评价"><a href="#个人评价" class="headerlink" title="个人评价"></a>个人评价</h1><p>​    这本书从新手入门的角度可以说是一本很适合的书，讲的内容是非常基础但是可以让你刚好入门的程度，翻起来也不会十分的痛苦，同时在内容的编排方面也是典型的<strong>由浅入深</strong>，这本书 <strong>没有什么废话</strong>，基本就是直接用案例告诉你sql如何使用，对于新手来说是特别友好的一本书。</p>
<p>​    另外说明一下书中介绍的mysql5.1是非常老的版本，<strong>所以旧版本的特性完全不建议深入学习</strong>，而是应该多看看<strong>mysql5.5</strong>之后的版本新特性，当然如果公司有遗留项目使用低版本的mysql，这里有些内容还是有一定的参考价值的（仅适用于mysql5.1）。</p>
<h1 id="内容概要"><a href="#内容概要" class="headerlink" title="内容概要"></a>内容概要</h1><p>​    实操大于理论的一本书，这里提一些日常比较容易忽略的一些点：</p>
<h2 id="尾空格的like"><a href="#尾空格的like" class="headerlink" title="尾空格的like"></a>尾空格的like</h2><p>​    一条记录存储的格式如下，如果此时写入一条如下的查询条件<code>like %anvil</code>是<strong>不会匹配上第二条记录</strong>的，因为尾部空格的缘故，导致第二条记录是检索不到的，这里的解决办法是可以使用<code>trim()</code>函数或者写成<code>%anvil%</code>的方式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">prod_id | prod_name</span><br><span class="line">a1			2 ton anvil</span><br><span class="line">a2			4 ton anvil[空格]</span><br><span class="line">a3 			1 ton anvil</span><br></pre></td></tr></table></figure>

<p>​    其实这种错误只要入库的时候数据进行严格的处理一般不会出什么问题，这里引申一下java中一个还算比较容易犯的错误：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    Map&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    map.put(<span class="string">" a1"</span>, <span class="string">"test1"</span>);</span><br><span class="line">    map.put(<span class="string">"a3"</span>, <span class="string">"test3"</span>);</span><br><span class="line">    map.put(<span class="string">"a2"</span>, <span class="string">"test2"</span>);</span><br><span class="line">    System.out.println(map.get(<span class="string">"a1"</span>));</span><br><span class="line">&#125;<span class="comment">/*运行结果：</span></span><br><span class="line"><span class="comment">null</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<p>​    不知道碰到这种BUG的人有多痛苦，个人没有碰到过，但是曾经给同事排查问题的发现了类似的情况，当然不是这么明显的错，而是由于<strong>前端传递</strong>的时候，json当中的key前面多了一个空格，也是因为这个空格，导致花了好几个小时才排查出来！这里希望读者<strong>引以为戒</strong>，在编写类似代码时候严格检查有没有手贱多敲空格。</p>
<h2 id="and和or的优先级问题："><a href="#and和or的优先级问题：" class="headerlink" title="and和or的优先级问题："></a>and和or的优先级问题：</h2><p>​    有时候我们会写出这样的sql：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> t_user <span class="keyword">where</span> <span class="keyword">name</span> = <span class="string">'xxx'</span> <span class="keyword">or</span> age &gt; <span class="number">18</span> <span class="keyword">and</span> email <span class="keyword">like</span> <span class="string">'@qq.com'</span></span><br></pre></td></tr></table></figure>

<p>​    这里根据优先级的就近匹配原则，<strong>百分之百的会出现意想不到的情况</strong>，因为此时mysql会误认为是下面这样的情况，进行查询之后，数据肯定和我们预想的or条件不一致了：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> t_user <span class="keyword">where</span> <span class="keyword">name</span> = <span class="string">'xxx'</span> <span class="keyword">or</span> (age &gt; <span class="number">18</span> <span class="keyword">and</span> email <span class="keyword">like</span> <span class="string">'@qq.com'</span>)</span><br></pre></td></tr></table></figure>

<p>​    要修复这段程序很简单就是把or查询的条件包裹起来：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> t_user <span class="keyword">where</span> (<span class="keyword">name</span> = <span class="string">'xxx'</span> <span class="keyword">or</span> age &gt; <span class="number">18</span>) <span class="keyword">and</span> email <span class="keyword">like</span> <span class="string">'@qq.com'</span></span><br></pre></td></tr></table></figure>

<p>​    所以不管是编程语言还是sql，都需要小心这种操作符优先级带来的莫名其妙的问题。</p>
<h2 id="不能没有的where条件"><a href="#不能没有的where条件" class="headerlink" title="不能没有的where条件"></a>不能没有的where条件</h2><p>​    不论是select，update，insert，delete那个操作，最应该关注的是有没有<strong>where</strong>条件，可能有人会觉得select影响会稍微小一些？</p>
<p>​    大错特错，加入select一张全表特别是<code>select *</code>一个表所有的内容并且通过反射映射到程序当中的时候，甚至会导致 <strong>内存溢出</strong>，并且这种事情是真实发生过的事情。</p>
<p>​    另外提一个建议是<strong>mybatis</strong>编写一些插件对于查询语句进行扫描，<strong>排查没有where条件的sql</strong>并且不允许执行。</p>
<p>​    上面扯远了，总之，不管是平时练习还是自己上手干活的时候，一定要确保可以触发<strong>where条件</strong>。</p>
<h2 id="小心NULL值"><a href="#小心NULL值" class="headerlink" title="小心NULL值"></a>小心NULL值</h2><p>​    <strong>null</strong>这个值大概是所有人痛恨的一个东西，然而也是十分重要的一个值，在数据库操作的时候尤其需要小心，因为某些函数比如<code>count()，sum()、min()、max()</code>等函数是不会统计<code>null</code>的数据的，这很容易造成统计数据的错误。</p>
<h2 id="逗号的陷阱"><a href="#逗号的陷阱" class="headerlink" title="逗号的陷阱"></a>逗号的陷阱</h2><p>​    逗号也是非常容易犯错的一个点，当一条sql几十上百行的时候，如果那里写错一个逗号排查难度可想而知，当然mysql给的提示多数情况能发现问题，但是更多的时候会给出一些莫名其妙的错误，报错的结果和真实的错误相差甚远，所以建议每写一点sql就应该马上运行一下，查看一下逗号是否写错，以及是否写多或者语法的问题。</p>
<p>​    写了一大段的sql语句并且自我感觉良好运行的时候，这种错误尤其容易出现。</p>
<h2 id="having和where的区别"><a href="#having和where的区别" class="headerlink" title="having和where的区别"></a>having和where的区别</h2><p>​    书中的介绍非常的简洁易懂，这里直接摘抄了：<strong>where在数据分组之前进行过滤，having在数据分组之后进行过滤。where排除的行不包含在分组中</strong>，这容易改变计算值，从而影响having的计算结果。</p>
<h2 id="NOT语句的限制"><a href="#NOT语句的限制" class="headerlink" title="NOT语句的限制"></a>NOT语句的限制</h2><p>​    mysql的Not语句限制比较大，只能和<strong>in、between、exists</strong>语句进行匹配，不像<strong>其他某些的sql语言可以对于判断条件进行取反</strong></p>
<h2 id="NULL-和-IS-NULL"><a href="#NULL-和-IS-NULL" class="headerlink" title="= NULL 和 IS NULL"></a>= NULL 和 IS NULL</h2><p>​    老生常谈的一个话题，这里不要去纠结，mysql中判断字段内容是否为null就应该使用 is null，使用 = null 容易出现意想不到的结果。</p>
<h2 id="Rtrim-和-Ltrim-语句"><a href="#Rtrim-和-Ltrim-语句" class="headerlink" title="Rtrim 和 Ltrim 语句"></a>Rtrim 和 Ltrim 语句</h2><p>​    除了基本的trim函数，mysql还支持删除数据左边的空格或者右边的空格，还是挺方便的，这里也记录到了笔记上面。</p>
<h2 id="更新和删除的指导原则："><a href="#更新和删除的指导原则：" class="headerlink" title="更新和删除的指导原则："></a>更新和删除的指导原则：</h2><p>​    好的建议总是让人忍不住点赞，这里也同样记录：</p>
<ul>
<li><p>除非全表更新或者删除，否则总是使用where条件更新或者删除</p>
</li>
<li><p>保证每一个表都有主键，尽量使用主键关联删除，因为主键索引的效率十分高</p>
</li>
<li><p>在where条件更新或者删除之前，<strong>先用SELECT实验一遍</strong>。不要对自己的sql过于自信</p>
</li>
<li><p>对于一些关键数据进行表强制关联引用，防止误删除。</p>
<p>这一点可能很难做到，因为现代多数设计很少使用外键而是使用冗余字段+业务去处理的。</p>
</li>
</ul>
<h2 id="更快的删表方法TRUNCATE"><a href="#更快的删表方法TRUNCATE" class="headerlink" title="更快的删表方法TRUNCATE"></a>更快的删表方法TRUNCATE</h2><p>​    书中给了一个特殊的删表方法，这个还真用的不多，他的格式如下，不建议随便乱用，如果小心把重要数据表干了这里表示不负责哈……</p>
<p>​    这个删除的原理其实是把整个表干掉，然后直接重新建立一个表，有点类似neo4j图数据的操作…..</p>
 <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">TRUNCATE</span> <span class="keyword">table</span> task_log</span><br></pre></td></tr></table></figure>

<p>​    现实情况下其实是<strong>完全不推荐</strong>使用delete去大批量的删除数据，很容易造成资源占用和线程的阻塞（涉及数据结构和底层设计的原理，原因很复杂不做扩展）。也有一些线上库是禁止DELETE的或者在设计的层面上引入 <strong>假删除</strong>，这里也不做过多扩展，总之<strong>DELETE操作能少用就少用</strong>。</p>
<h2 id="建表的时候尽量使用not-null"><a href="#建表的时候尽量使用not-null" class="headerlink" title="建表的时候尽量使用not null"></a>建表的时候尽量使用not null</h2><p>​    又是这个深恶痛绝的null，有些数据表的设计会默认给一个值，哪怕他确实没有任何数据也要填充一个’’空字符串进去，至于原因这里忘了，总之是null对于索引有很重要的影响，容易让索引失效？总之，<strong>数据最好都有意义，没有意义就给个默认值是最好的</strong>。</p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>​    如果没有mysql或者sql基础，这本书算是最值得推荐的入门书籍，没有之一，个人当年学习数据库是用的一些垃圾培训机构的书，写的又啰嗦还全是图片，后面跑去看培训机构的视频才真正看懂…… </p>
<h1 id="写到最后"><a href="#写到最后" class="headerlink" title="写到最后"></a>写到最后</h1><p>​    写笔记的时候回过头又看了一遍，确实是一本值得推荐的好书，好拿读起来也十分的轻松。</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>lazytime
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2021/10/16/mysqlbizhi/" title="《MySQL必知必会》读书笔记">https://whitestore.top/2021/10/16/mysqlbizhi/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Mysql/" rel="tag"># Mysql</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/17/weixiguan/" rel="prev" title="《微习惯》读书笔记">
      <i class="fa fa-chevron-left"></i> 《微习惯》读书笔记
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/10/16/lajihuishou/" rel="next" title="深入理解JVM - 分代与垃圾回收">
      深入理解JVM - 分代与垃圾回收 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#《MySQL必知必会》读书笔记"><span class="nav-number">1.</span> <span class="nav-text">《MySQL必知必会》读书笔记</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">2.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#资源链接："><span class="nav-number">3.</span> <span class="nav-text">资源链接：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#推荐语"><span class="nav-number">4.</span> <span class="nav-text">推荐语</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#个人评价"><span class="nav-number">5.</span> <span class="nav-text">个人评价</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#内容概要"><span class="nav-number">6.</span> <span class="nav-text">内容概要</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#尾空格的like"><span class="nav-number">6.1.</span> <span class="nav-text">尾空格的like</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#and和or的优先级问题："><span class="nav-number">6.2.</span> <span class="nav-text">and和or的优先级问题：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#不能没有的where条件"><span class="nav-number">6.3.</span> <span class="nav-text">不能没有的where条件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小心NULL值"><span class="nav-number">6.4.</span> <span class="nav-text">小心NULL值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#逗号的陷阱"><span class="nav-number">6.5.</span> <span class="nav-text">逗号的陷阱</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#having和where的区别"><span class="nav-number">6.6.</span> <span class="nav-text">having和where的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#NOT语句的限制"><span class="nav-number">6.7.</span> <span class="nav-text">NOT语句的限制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#NULL-和-IS-NULL"><span class="nav-number">6.8.</span> <span class="nav-text">&#x3D; NULL 和 IS NULL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Rtrim-和-Ltrim-语句"><span class="nav-number">6.9.</span> <span class="nav-text">Rtrim 和 Ltrim 语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#更新和删除的指导原则："><span class="nav-number">6.10.</span> <span class="nav-text">更新和删除的指导原则：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#更快的删表方法TRUNCATE"><span class="nav-number">6.11.</span> <span class="nav-text">更快的删表方法TRUNCATE</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#建表的时候尽量使用not-null"><span class="nav-number">6.12.</span> <span class="nav-text">建表的时候尽量使用not null</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">7.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#写到最后"><span class="nav-number">8.</span> <span class="nav-text">写到最后</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
